package com.fc.v2.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.text.StrBuilder;
import com.fc.v2.mapper.auto.*;
import com.fc.v2.model.Examine;
import com.fc.v2.model.ExpertSelect;
import com.fc.v2.model.auto.*;
import com.fc.v2.shiro.util.ShiroUtils;
import com.fc.v2.util.ECertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 结项审核申请 FinishProjectService
 * @Title: FinishProjectService.java 
 * @Package com.fc.v2.service 
 * @author Archie_自动生成
 * @email ${email}
 * @date 2021-11-08 11:54:59  
 **/
@Service
public class FinishProjectService implements BaseService<FinishProject, FinishProjectExample>{
	@Autowired
	private FinishProjectMapper finishProjectMapper;

	@Autowired
	private TsysUserMapper tsysUserMapper;

	@Autowired
	private ExperprojectMapper experprojectMapper;

	@Autowired
	private FinishUploadMapper finishUploadMapper;

	@Autowired
	private ApplyParticipatorService applyParticipatorService;

	@Autowired
	private TSysRoleUserMapper tSysRoleUserMapper;
      	   	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<FinishProject> list(Tablepar tablepar,FinishProject finishProject){
	        FinishProjectExample testExample=new FinishProjectExample();
		 	FinishProjectExample.Criteria criteria = null;
		 	String userId = ShiroUtils.getUserId();
			 //项目名称搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
				 String name = "%"+tablepar.getSearchText()+"%";
				 criteria = testExample.createCriteria().andProNameLike(name).andManIdEqualTo(userId);
			 }else {//大搜索
				 criteria = testExample.createCriteria().andLikeQuery(finishProject).andManIdEqualTo(userId);
			 }
			 //项目类型搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchType())) {//小窗体
				 criteria.andProTypeChildEqualTo(tablepar.getSearchType()).andManIdEqualTo(userId);}
			 //项目编号搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchNum())) {//小窗体
				 //模糊搜索
				 String num = "%"+tablepar.getSearchNum()+"%";
				 criteria.andProNumberLike(num).andManIdEqualTo(userId);}
			 //学科分类搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchSub())) {//小窗体
				 criteria.andProSubjectEqualTo(tablepar.getSearchSub()).andManIdEqualTo(userId);}
			 //申报单位搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchIns())) {//小窗体
				 String ins = "%"+tablepar.getSearchIns()+"%";
				 criteria.andProDeptLike(ins).andManIdEqualTo(userId);}
			 //负责人搜索
			 if(StrUtil.isNotEmpty(tablepar.getSearchMan())) {//小窗体
				 String man = "%"+tablepar.getSearchMan()+"%";
				 criteria.andManNameLike(man).andManIdEqualTo(userId);}
			//表格排序
			//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        //}else{
	        //	testExample.setOrderByClause("id ASC");
	        //}
	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<FinishProject> list= finishProjectMapper.selectByExample(testExample);
	        PageInfo<FinishProject> pageInfo = new PageInfo<FinishProject>(list);
	        return  pageInfo;
	 }

	/**
	 * 证书项目查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<FinishProject> listCert(Tablepar tablepar,FinishProject finishProject){
		FinishProjectExample testExample=new FinishProjectExample();
		FinishProjectExample.Criteria criteria = null;

		//项目名称搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			String name = "%"+tablepar.getSearchText()+"%";
			criteria = testExample.createCriteria().andProNameLike(name).andCertStateEqualTo(Examine.PASSED.code);
		}else {//大搜索
			criteria = testExample.createCriteria().andLikeQuery(finishProject).andCertStateEqualTo(Examine.PASSED.code);
		}
		//项目类型搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchType())) {//小窗体
			criteria.andProTypeChildEqualTo(tablepar.getSearchType()).andCertStateEqualTo(Examine.PASSED.code);}
		//项目编号搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchNum())) {//小窗体
			//模糊搜索
			String num = "%"+tablepar.getSearchNum()+"%";
			criteria.andProNumberLike(num).andCertStateEqualTo(Examine.PASSED.code);}
		//学科分类搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchSub())) {//小窗体
			criteria.andProSubjectEqualTo(tablepar.getSearchSub()).andCertStateEqualTo(Examine.PASSED.code);}
		//申报单位搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchIns())) {//小窗体
			String ins = "%"+tablepar.getSearchIns()+"%";
			criteria.andProDeptLike(ins).andCertStateEqualTo(Examine.PASSED.code);}
		//负责人搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchMan())) {//小窗体
			String man = "%"+tablepar.getSearchMan()+"%";
			criteria.andManNameLike(man).andCertStateEqualTo(Examine.PASSED.code);}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<FinishProject> list= finishProjectMapper.selectByExample(testExample);
		PageInfo<FinishProject> pageInfo = new PageInfo<FinishProject>(list);
		return  pageInfo;
	}

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<FinishProject> listFinishExpert(Tablepar tablepar,FinishProject finishProject){
		FinishProjectExample testExample=new FinishProjectExample();
		FinishProjectExample.Criteria criteria = null;
		//项目中专家审核字段为待审核或完成审核
		List<Integer> inList = new ArrayList<Integer>();
		inList.add(Examine.NEED_EXAMINE.code);
		inList.add(Examine.PASSED.code);
		//获取当前专家用户id
		String userId = ShiroUtils.getUserId();
		ExperprojectExample epExample = new ExperprojectExample();
		//与专家对应的项目id
		epExample.createCriteria().andExpertIdEqualTo(userId).andOther1EqualTo(ExpertSelect.EXPERT_FINISH.code);
		List<Experproject> listPro = new ArrayList<>();
		listPro = experprojectMapper.selectByExample(epExample);
		List<String> proIdList = new ArrayList<>();
		for (Experproject ep:listPro) {
			proIdList.add(ep.getProId());
		}
		//为List赋初值，避免size=0报错
		proIdList.add("0");

		//项目名称搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			String name = "%"+tablepar.getSearchText()+"%";
			criteria = testExample.createCriteria().andProNameLike(name).andExpertPassIn(inList).andIdIn(proIdList);
		}else {//大搜索
			criteria = testExample.createCriteria().andIdIn(proIdList).andExpertPassIn(inList);
		}
		//项目类型搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchType())) {//小窗体
			criteria.andProTypeEqualTo(tablepar.getSearchType()).andExpertPassIn(inList).andIdIn(proIdList);}
		//项目编号搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchNum())) {//小窗体
			//模糊搜索
			String num = "%"+tablepar.getSearchNum()+"%";
			criteria.andProNumberLike(num).andExpertPassIn(inList).andIdIn(proIdList);}
		//学科分类搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchSub())) {//小窗体
			criteria.andProSubjectLike(tablepar.getSearchSub()).andExpertPassIn(inList).andIdIn(proIdList);}
		//申报单位搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchIns())) {//小窗体
			String ins = "%"+tablepar.getSearchIns()+"%";
			criteria.andProDeptLike(ins).andExpertPassIn(inList).andIdIn(proIdList);}
		//负责人搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchMan())) {//小窗体
			String man = "%"+tablepar.getSearchMan()+"%";
			criteria.andManNameLike(man).andExpertPassIn(inList).andIdIn(proIdList);}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<FinishProject> list= finishProjectMapper.selectByExample(testExample);
		PageInfo<FinishProject> pageInfo = new PageInfo<FinishProject>(list);
		return  pageInfo;
	}

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<FinishProject> listSKL(Tablepar tablepar,FinishProject finishProject){
		FinishProjectExample testExample=new FinishProjectExample();
		FinishProjectExample.Criteria criteria = null;

		//待审核、不通过、通过、专家审核中、专家审核完成状态
		List<Integer> inList = new ArrayList<>();
		inList.add(Examine.NEED_EXAMINE.code);
		inList.add(Examine.PASSED.code);
		inList.add(Examine.BANNED.code);
		inList.add(Examine.EXPECT_DOING.code);
		inList.add(Examine.EXPECT_DONE.code);

		//项目名称搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			String name = "%"+tablepar.getSearchText()+"%";
			criteria = testExample.createCriteria().andProNameLike(name).andSklPassIn(inList);
		}else {//大搜索
			criteria = testExample.createCriteria().andLikeQuery(finishProject).andSklPassIn(inList);
		}
		//项目类型搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchType())) {//小窗体
			criteria.andProTypeEqualTo(tablepar.getSearchType()).andSklPassIn(inList);}
		//项目编号搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchNum())) {//小窗体
			//模糊搜索
			String num = "%"+tablepar.getSearchNum()+"%";
			criteria.andProNumberLike(num).andSklPassIn(inList);}
		//学科分类搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchSub())) {//小窗体
			criteria.andProSubjectEqualTo(tablepar.getSearchSub()).andSklPassIn(inList);}
		//申报单位搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchIns())) {//小窗体
			String ins = "%"+tablepar.getSearchIns()+"%";
			criteria.andProDeptLike(ins).andSklPassIn(inList);}
		//负责人搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchMan())) {//小窗体
			String man = "%"+tablepar.getSearchMan()+"%";
			criteria.andManNameLike(man).andSklPassIn(inList);}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<FinishProject> list= finishProjectMapper.selectByExample(testExample);
		PageInfo<FinishProject> pageInfo = new PageInfo<FinishProject>(list);
		return  pageInfo;
	}

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<FinishProject> listDanwei(Tablepar tablepar,FinishProject finishProject){
		FinishProjectExample testExample=new FinishProjectExample();
		FinishProjectExample.Criteria criteria = null;
		//获取当前用户id
		String userId = ShiroUtils.getUser().getId();
		//通过当前用户id查找所在单位名称
		String deptName = tsysUserMapper.selectByPrimaryKey(userId).getDeptName();
		//单位审核状态
		List<Integer> stateList = new ArrayList<>();
		stateList.add(Examine.NEED_EXAMINE.code);
		stateList.add(Examine.PASSED.code);
		stateList.add(Examine.BANNED.code);
		stateList.add(Examine.NEED_SUBMIT.code);
		//项目名称搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			String name = "%"+tablepar.getSearchText()+"%";
			criteria = testExample.createCriteria().andProNameLike(name).andProDeptEqualTo(deptName).andDeptPassIn(stateList);
		}else {//大搜索
			criteria = testExample.createCriteria().andLikeQuery(finishProject).andProDeptEqualTo(deptName).andDeptPassIn(stateList);
		}
		//项目类型搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchType())) {//小窗体
			criteria.andProTypeChildEqualTo(tablepar.getSearchType()).andProDeptEqualTo(deptName).andDeptPassIn(stateList);}
		//项目编号搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchNum())) {//小窗体
			//模糊搜索
			String num = "%"+tablepar.getSearchNum()+"%";
			criteria.andProNumberLike(num).andProDeptEqualTo(deptName).andDeptPassIn(stateList);}
		//学科分类搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchSub())) {//小窗体
			criteria.andProSubjectEqualTo(tablepar.getSearchSub()).andProDeptEqualTo(deptName).andDeptPassIn(stateList);}
		//申报单位搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchIns())) {//小窗体
			String ins = "%"+tablepar.getSearchIns()+"%";
			criteria.andProDeptLike(ins).andProDeptEqualTo(deptName).andDeptPassIn(stateList);}
		//负责人搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchMan())) {//小窗体
			String man = "%"+tablepar.getSearchMan()+"%";
			criteria.andManNameLike(man).andProDeptEqualTo(deptName).andDeptPassIn(stateList);}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<FinishProject> list= finishProjectMapper.selectByExample(testExample);
		PageInfo<FinishProject> pageInfo = new PageInfo<FinishProject>(list);
		return  pageInfo;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=ConvertUtil.toListStrArray(ids);
			FinishProjectExample example=new FinishProjectExample();
			example.createCriteria().andIdIn(lista);
			return finishProjectMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public FinishProject selectByPrimaryKey(String id) {
				
			return finishProjectMapper.selectByPrimaryKey(id);
				
	}

	/**
	 * 申报人修改提交
	 * @param record
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(FinishProject record) {
		//给单位添加待审核状态
		record.setDeptPass(Examine.NEED_EXAMINE.code);
		//给项目添加审核中状态
		record.setProState(Examine.NEED_EXAMINE.code);
		//更新时间
		record.setManUpdateTime(new Date());

		return finishProjectMapper.updateByPrimaryKeySelective(record);
	}


	/**
	 * 申报人修改保存
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKeySelectiveSave(FinishProject record) {
		//给项目添加审核中状态
		record.setProState(Examine.NEED_SUBMIT.code);
		//更新时间
		record.setManUpdateTime(new Date());

		return finishProjectMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 单位修改提交
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKeySelectiveDanwei(FinishProject record) {

		//更新时间
		record.setDeptUpdateTime(new Date());

		return finishProjectMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 社科联修改提交
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKeySelectiveSkl(FinishProject record, String id, HttpServletRequest request, HttpServletResponse response) {

		//更新时间
		record.setSklUpdateTime(new Date());
		//结项文件上传
		FinishUploadExample fuExample = new FinishUploadExample();
		fuExample.createCriteria().andFinishIdEqualTo(id);
		FinishUpload finishUpload = finishUploadMapper.selectByExample(fuExample).get(0);
		//上传文件路径
		String proveUrl = finishUpload.getProveUrl();
		//若审核通过则生成证书
		if(record.getSklPass() == Examine.PASSED.code){
			//生成证书状态为已生成
			record.setCertState(Examine.PASSED.code);
			record.setCertUpdateTime(new Date());
			//获取项目名称
			String proName = record.getProName();
			//获取项目批准号
			String proveNum = record.getProNumber();
			//获取负责人名称
			String manName = record.getManName();
			//获取鉴定等级
			String rank = record.getProSklRank();
			//获取成员
			ApplyParticipatorExample example = new ApplyParticipatorExample();
			example.createCriteria().andFinishIdEqualTo(id);
			List<ApplyParticipator> applyParticipatorList = new ArrayList<>();
			applyParticipatorList =  applyParticipatorService.selectByExample(example);
			StrBuilder participatorBuilder = new StrBuilder();
			for (ApplyParticipator ap:applyParticipatorList) {
				participatorBuilder.append(ap.getPartName()+" ");
			}
			ECertUtils.generateECert(request,response,proName,proveNum,manName,rank,participatorBuilder.toString());
		}
		record.setProveUrl(proveUrl);

		return finishProjectMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 提交
	 */
	@Override
	public int insertSelective(FinishProject record) {

		return finishProjectMapper.insertSelective(record);
	}

	/**
	 * 添加提交
	 */
	public int insertSelectiveSub(FinishProject record,String id) {
		//添加项目id
		record.setId(id);
		//给项目添加审核中状态
		record.setProState(Examine.NEED_EXAMINE.code);
		//将当前用户添加为负责人Id
		record.setManId(ShiroUtils.getUserId());
		//结项文件上传
		FinishUploadExample fuExample = new FinishUploadExample();
		fuExample.createCriteria().andFinishIdEqualTo(id);
		FinishUpload finishUpload = finishUploadMapper.selectByExample(fuExample).get(0);
		//上传文件路径
		String sumUrl = finishUpload.getSumUrl();
		String dupUrl = finishUpload.getDupUrl();
		String resUrl = finishUpload.getResUrl();
		String freeCheckUrl = finishUpload.getFreeCheckUrl();
		record.setSummaryReportUrl(sumUrl);
		record.setDupilicateUrl(dupUrl);
		record.setResContentUrl(resUrl);
		record.setFreeCheckUrl(freeCheckUrl);
		//更新时间
		record.setManUpdateTime(new Date());
		//判断用户是否为群众或单位申报人员
		TSysRoleUserExample tSysRoleUserExample = new TSysRoleUserExample();
		tSysRoleUserExample.createCriteria().andSysUserIdEqualTo(ShiroUtils.getUserId());
		List<TSysRoleUser> tSysRoleUserList = tSysRoleUserMapper.selectByExample(tSysRoleUserExample);
		for (TSysRoleUser ru:tSysRoleUserList) {
			//用户为单位申报人员
			if (ru.getSysRoleId().equals("620402627791949824")) {
				//给单位添加待审核状态
				record.setDeptPass(Examine.NEED_EXAMINE.code);
			}
			//群众
			else{
				//单位审核默认通过
				record.setDeptPass(Examine.PASSED.code);
			}
		}
		return finishProjectMapper.insertSelective(record);
	}

	/**
	 * 保存
	 */
	public int insertSelectiveSave(FinishProject record,String id) {

		//添加项目id
		record.setId(id);
		//给项目添加待提交状态
		record.setProState(Examine.NEED_SUBMIT.code);
		//将当前用户添加为负责人Id
		record.setManId(ShiroUtils.getUserId());
		//更新时间
		record.setManUpdateTime(new Date());
		//结项文件上传
		FinishUploadExample fuExample = new FinishUploadExample();
		fuExample.createCriteria().andFinishIdEqualTo(id);
		FinishUpload finishUpload = finishUploadMapper.selectByExample(fuExample).get(0);
		//上传文件路径
		String sumUrl = finishUpload.getSumUrl();
		String dupUrl = finishUpload.getDupUrl();
		String resUrl = finishUpload.getResUrl();
		String freeCheckUrl = finishUpload.getFreeCheckUrl();

		record.setSummaryReportUrl(sumUrl);
		record.setDupilicateUrl(dupUrl);
		record.setResContentUrl(resUrl);
		record.setFreeCheckUrl(freeCheckUrl);


		return finishProjectMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(FinishProject record, FinishProjectExample example) {
		
		return finishProjectMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(FinishProject record, FinishProjectExample example) {
		
		return finishProjectMapper.updateByExample(record, example);
	}

	@Override
	public List<FinishProject> selectByExample(FinishProjectExample example) {
		
		return finishProjectMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(FinishProjectExample example) {
		
		return finishProjectMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(FinishProjectExample example) {
		
		return finishProjectMapper.deleteByExample(example);
	}
	
	/**
	 * 修改权限状态展示或者不展示
	 * @param finishProject
	 * @return
	 */
	public int updateVisible(FinishProject finishProject) {
		return finishProjectMapper.updateByPrimaryKeySelective(finishProject);
	}


}
